package com.baoming.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baoming.dao.IMemberDao;
import com.baoming.domain.Member;
import com.baoming.service.IMemberService; 
/**
 * 会员组件实现类 只可读操作
 * @author Administrator
 *
 */
@Transactional(readOnly=true)
@Service(IMemberService.SERVICE)
public class MemberServiceImpl implements IMemberService {
	
	@Resource(name=IMemberDao.SERVICE)
	private IMemberDao memberDao ;

	/**
	 * 保存会员
	 */
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void saveMember(Member member) {
		member.setPassword(member.getPassword()) ;
		member.setRemember(member.getPassword()) ; 
		memberDao.save(member) ; 
	}

	/**
	 * 查询用户邮箱是否存在 如果存在 则返回true 否则返回false
	 */
	public boolean isHas(String email) {
		String hql = " and m.email = ? " ; 
		List<String> params = new ArrayList<String>() ; 
		params.add(email) ; 
		List<Member> members = memberDao.findCollectionByConditionNoPage(hql, params.toArray(), null) ; 
		if(members.size()>0){
			return true ; 
		}
		return false;
	}

	/**
	 * 用户登陆查询
	 */
	public Member checkLoginMember(String email , String password) {
		String hql = " and m.email = ? and password = ? and m.isDelete = 1 " ; 
		List<String> params = new ArrayList<String>() ; 
		params.add(email) ; 
		params.add(password) ;  
		List<Member> members = memberDao.findCollectionByConditionNoPage(hql, params.toArray(), null) ; 
		if(members != null && members.size() > 0){
			return members.get(0) ; 
		}
		return null;
	}

	/**
	 * 更新用户信息 修改信息
	 */
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void updateMemberMsg(Member gmember) {
		memberDao.updateMember(gmember) ; 
	}
	
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void updateMember(Member gmember) {
		memberDao.update(gmember) ; 
	}

	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void updateMemberPwd(String newPwd , String id) {
		memberDao.updateMemberPwd(newPwd , id) ; 
	}

	public Member selectMember(String memberId) {
		return memberDao.findObjectById(memberId);
	}

	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void updateMemberLoginTime(String lastLoginTime , String memberId) {
		memberDao.updateMemberLoginTime(lastLoginTime ,memberId) ; 
	} 
}
